package hibernate;

import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.query.Query;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class DbsOperation {
	private SessionFactory sessionFactory;
	private Session session;
	private Transaction transaction;
	public DbsOperation() {
		// TODO Auto-generated constructor stub
	}
	
	@Before
	public void init() {
		System.out.println("init");
		Configuration configuration = new Configuration().configure();
		sessionFactory = configuration.buildSessionFactory();
		session = sessionFactory.openSession();
		transaction = session.beginTransaction();
		
	}

	@After
	public void destory() {
		transaction.commit(); 
		session.close();
		sessionFactory.close();
		System.out.println("destory");
	}
	
	@Test
	public void testAddStudent() {
		System.out.println("test");
		Student stu = new Student("czx", "123456");
		session.save(stu);
		
	}
	@Test
	public void testAddTeacher() {
		System.out.println("test");
		Teacher tec = new Teacher("czx","123456");
		session.save(tec);
		
	}
	@Test
	public void test() {
		System.out.println("test");
		Teacher teacher = session.get(Teacher.class,1);
		
		Student student = new Student("student1","password");
		teacher.getStudents().add(student);
		
		session.save(student);
		session.save(teacher);

		System.out.println(teacher.toString());

		
	}
	

	public Student getStudent(Integer id) {

		init();
		Query query = session.getNamedQuery("get_student_by_id"); 
		query.setParameter("id", id);

		List<Student> list = query.list();
		Student student = list.get(0);
		
		
		System.out.println(student);
		destory();
		return student;

	}
	
	public Teacher getTeacher(Integer id) {

		init();
		Teacher teacher = session.get(Teacher.class, id);
		destory();
		return teacher;

	}
	

	public void updateStudent(Student student) {
		init();
		session.update(student);
		destory();
	}
	public void updateTecher(Teacher teacher) {
		init();
		session.update(teacher);
		destory();
	}
	public StringBuffer getStudents(Integer id) {
		init();
		Teacher teacher = session.get(Teacher.class, id);
		StringBuffer sb = new StringBuffer();
		Set<Student> students = teacher.getStudents();
		Iterator<Student> it = students.iterator();
		while(it.hasNext()) {
			sb.append(it.next().getS_ID() + "\n");
		}
		destory();
		return sb;
		
	}
	public void TeacherAddStudent(Integer id,Student student) {
		init();
		Teacher teacher = session.get(Teacher.class, id);

		teacher.getStudents().add(student);

		session.update(teacher);	
		destory();
	}
	public void TeacherDeleteStudent(Integer t_id,Integer s_id) {
		init();
		Teacher teacher = session.get(Teacher.class, t_id);
		System.out.println(teacher.getStudents().size());
		Student student = session.get(Student.class, s_id);
		teacher.getStudents().remove(student);
		System.out.println(teacher.getStudents().size());
		session.update(teacher);	
		destory();
	}
	@Test
	public void test1() {

		Teacher teacher = session.get(Teacher.class, 1);
		System.out.println(teacher.getStudents().size());
		Student student = session.get(Student.class, 12);
		teacher.getStudents().remove(student);
		System.out.println(teacher.getStudents().size());
		session.update(teacher);	

	}
	public void AddStudent(Student student) {
		init();
		session.save(student);
		destory();
		
	}
	public void AddTeacher(Teacher teacher) {
		init();
		session.save(teacher);
		destory();
		
	}
	public void DeleteStudent(Student student) {
		init();
		session.delete(student);
		destory();
		
	}
	public void DeleteTeacher(Teacher teacher) {
		init();
		session.delete(teacher);
		destory();
		
	}
}
